package cn.stylefeng.guns.modular.basic.controller;

import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.base.shiro.ShiroUser;
import cn.stylefeng.guns.modular.basic.entity.BasicPutgoods;
import cn.stylefeng.guns.modular.basic.entity.BasicWarehouse;
import cn.stylefeng.guns.modular.basic.entity.StoreAccount;
import cn.stylefeng.guns.modular.basic.model.params.BasicPutgoodsParam;
import cn.stylefeng.guns.modular.basic.model.params.BasicWarehouseParam;
import cn.stylefeng.guns.modular.basic.model.params.StoreAccountParam;
import cn.stylefeng.guns.modular.basic.model.params.StoreAccountRecordParam;
import cn.stylefeng.guns.modular.basic.model.result.BasicPutgoodsResult;
import cn.stylefeng.guns.modular.basic.model.result.StoreAccountResult;
import cn.stylefeng.guns.modular.basic.service.BasicPutgoodsService;
import cn.stylefeng.guns.modular.basic.service.BasicWarehouseService;
import cn.stylefeng.guns.modular.basic.service.StoreAccountRecordService;
import cn.stylefeng.guns.modular.basic.service.StoreAccountService;
import cn.stylefeng.guns.sys.modular.system.entity.User;
import cn.stylefeng.roses.core.base.controller.BaseController;
import cn.stylefeng.roses.core.reqres.response.ResponseData;
import cn.stylefeng.guns.sys.modular.system.service.UserService;
import cn.stylefeng.roses.core.util.ToolUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

import static cn.stylefeng.guns.sys.core.shiro.ShiroKit.getUser;


/**
 * 门店账户控制器
 *
 * @author wp
 * @Date 2025-08-27 11:30:03
 */
@Controller
@RequestMapping("/storeAccount")
public class StoreAccountController extends BaseController {

    private String PREFIX = "/storeAccount";

    @Autowired
    private StoreAccountService storeAccountService;

    @Autowired
    private UserService userService;

    @Autowired
    private BasicWarehouseService basicWarehouseService;

    @Autowired
    private BasicPutgoodsService basicPutgoodsService;
    @Autowired
    private StoreAccountRecordService storeAccountRecordService;
    /**
     * 跳转到主页面
     *
     * @author wp
     * @Date 2025-08-27
     */
    @RequestMapping("")
    public String index() {
        return PREFIX + "/storeAccount.html";
    }

    /**
     * 新增页面
     *
     * @author wp
     * @Date 2025-08-27
     */
    @RequestMapping("/add")
    public String add(Model model) {
        User user = userService.getcurrentUser();
        BasicWarehouseParam basicWarehouseParam = new BasicWarehouseParam();
        basicWarehouseParam.setDeliverId(user.getDeliverId());
        Long deptId = user.getDeptId();
        LayuiPageInfo pageBySpec = this.basicWarehouseService.findPageBySpec(basicWarehouseParam, null, deptId, user.getOperateId());
        List<BasicWarehouse> warehouseList = (List<BasicWarehouse>) pageBySpec.getData();

        model.addAttribute("warehouseList", warehouseList);
        model.addAttribute("account", System.currentTimeMillis() / 1000);
        return PREFIX + "/storeAccount_add.html";
    }

    /**
     * 编辑页面
     *
     * @author wp
     * @Date 2025-08-27
     */
    @RequestMapping("/edit")
    public String edit() {
        return PREFIX + "/storeAccount_edit.html";
    }

    /**
     * 新增接口
     *
     * @author wp
     * @Date 2025-08-27
     */
    @RequestMapping("/addItem")
    @ResponseBody
    public ResponseData addItem(StoreAccountParam storeAccountParam) {

        StoreAccountResult listBySpec = this.storeAccountService.findListByStoreId(storeAccountParam.getStoreId());
        if (listBySpec!= null) {
            return ResponseData.error("该门店已存在账号，请勿重复添加！");
        }
        ShiroUser user = getUser();
        BasicPutgoods basicPutgoods = basicPutgoodsService.getById(storeAccountParam.getStoreId());
        storeAccountParam.setStoreName(basicPutgoods.getPutgoodsName());
        storeAccountParam.setCreateBy(user.getAccount());
        storeAccountParam.setCreateTime(new Date());
        this.storeAccountService.add(storeAccountParam);
        return ResponseData.success();
    }

    /**
     * 编辑接口
     *
     * @author wp
     * @Date 2025-08-27
     */
    @RequestMapping("/editItem")
    @ResponseBody
    public ResponseData editItem(StoreAccountParam storeAccountParam) {
        this.storeAccountService.update(storeAccountParam);
        return ResponseData.success();
    }

    /**
     * 删除接口
     *
     * @author wp
     * @Date 2025-08-27
     */
    @RequestMapping("/delete")
    @ResponseBody
    public ResponseData delete(StoreAccountParam storeAccountParam) {
        this.storeAccountService.delete(storeAccountParam);
        return ResponseData.success();
    }

    /**
     * 查看详情接口
     *
     * @author wp
     * @Date 2025-08-27
     */
    @RequestMapping("/detail")
    @ResponseBody
    public ResponseData detail(StoreAccountParam storeAccountParam) {
        StoreAccount detail = this.storeAccountService.getById(storeAccountParam.getId());
        return ResponseData.success(detail);
    }

    /**
     * 查询列表
     *
     * @author wp
     * @Date 2025-08-27
     */
    @ResponseBody
    @RequestMapping("/list")
    public LayuiPageInfo list(StoreAccountParam storeAccountParam,String condition ) {
        return this.storeAccountService.findPageBySpec(storeAccountParam,condition);
    }

    /**
     * 新增页面
     *
     * @author wp
     * @Date 2025-08-27
     */
    @RequestMapping("/rechargeDlg")
    public String rechargeDlg(Model model , Integer id) {

        StoreAccount storeAccount = this.storeAccountService.getById(id);
        model.addAttribute("account", storeAccount.getAccount());
        model.addAttribute("storeName", storeAccount.getStoreName());
        model.addAttribute("remainingSum", storeAccount.getRemainingSum());
        return PREFIX + "/storeAccount_recharge.html";
    }

    /**
     * 充值记录页面
     * @param model
     * @param id
     * @return
     */
    @RequestMapping("/rechargeRecordDlg")
    public String rechargeRecordDlg(Model model , Integer id) {

        StoreAccount storeAccount = this.storeAccountService.getById(id);
        model.addAttribute("account", storeAccount.getAccount());
        model.addAttribute("storeName", storeAccount.getStoreName());
        model.addAttribute("remainingSum", storeAccount.getRemainingSum());
        return PREFIX + "/storeAccount_recharge.html";
    }

    /**
     * 新增页面
     *
     * @author wp
     * @Date 2025-08-27
     */
    @RequestMapping("/recharge")
    @ResponseBody
    public ResponseData recharge(Integer account, Integer amount, BigDecimal remainingSum) {

        //更新账户金额
        StoreAccountParam storeAccountParam = new StoreAccountParam();
        storeAccountParam.setAccount(Integer.toString(account));
        storeAccountParam.setRemainingSum(remainingSum.add(new BigDecimal(amount)));
        storeAccountParam.setUpdateTime(new Date());
        storeAccountParam.setUpdateBy(getUser().getAccount());


        this.storeAccountService.updateByAccount(storeAccountParam);
        //  保存充值记录
        StoreAccountRecordParam storeAccountRecordParam = new StoreAccountRecordParam();
        storeAccountRecordParam.setAccount(Integer.toString(account));//充值账户
        storeAccountRecordParam.setCreateTime(new Date());
        storeAccountRecordParam.setCreateBy(getUser().getAccount());
        storeAccountRecordParam.setAmount(BigDecimal.valueOf(amount));
        storeAccountRecordParam.setRemainingSum(remainingSum.add(new BigDecimal(amount)));
        storeAccountRecordParam.setType("充值");
        StoreAccount byId = this.storeAccountService.getByAccount(account,null);

        storeAccountRecordParam.setStoreId(byId.getStoreId());
        storeAccountRecordParam.setStoreName(byId.getStoreName());
        this.storeAccountRecordService.add(storeAccountRecordParam);
        return ResponseData.success();
    }
}


